var dir = "";
$(function() {
	
	var scripts = document.getElementsByTagName('script');
	if(scripts.length){
	    // 获取最近一个加载的script，即这个js本身
	    var lastScript = scripts[scripts.length-1];
	    // 获取此js的路径  
	    //var scriptSrc = lastScript.src;
	    var scriptSrc = lastScript.src;
	    dir=getQueryString("dir",scriptSrc);
	}
	
	$("head  title").text(config.name);
	
	
	 if(!isFast()){
         document.getElementById("tip").removeAttribute("style");
         document.getElementById("tipmask").removeAttribute("style");
       }
       
	    $(document).keydown(function (event) {
	    	
	        if (event.keyCode == 13) {
	        	app.submit();
	        }
	      });
});

function isFast(){
    var explorer = window.navigator.userAgent.toLowerCase();
    if (window.navigator.userAgent.indexOf('compatible') !== -1) {
        return false
    }
    if(window.navigator.userAgent.indexOf('AppleWebKit') !== -1) {
        return true
    }else{
       return false
    }  
  }



var app = new Vue({
	el: '#app',
	data: {
		title: "",
		name:"",
		password:"",
		code:"",
		error:"",
	},
	mounted: function(){
		this.initData();
	},	
	
	methods: {
		initData: function(){
			var that = this;
			that.title=config.name;
			this.name = $("#name").val()==""?"":$("#name").val();
			this.refreshimg();
		},
		refreshimg:function() {
			tools.ajaxRequestAsync("checkCode", {}, function(data){
				$("#randomImage").attr("src", "data:image/png;base64,"+data.image_base64);
			    // $("#randomImage").attr("chkcode",data.chkcode);
			});
		},
		submit:function() {
			 var that=this;
			 if(this.name.trim()==""){
				 this.error="请输入登录名";
				 return false;
		     }
			 if(this.password.trim()==""){
			 	 this.error="请输入登录密码";
				 return false;
		     }
			 if(this.code.trim()==""){
				 this.error="请输入验证码";
				 return false;
		     }


			//  var chkcode= $("#randomImage").attr("chkcode");
		    //  if(this.code!=chkcode){
		    //  	this.error="验证码不正确";
		    //  	return false;
		    //  }
			var reqData = {
				"code": this.code
			};
			$("#error").empty();
			tools.ajaxRequestAsync("compareCode", reqData, function(data){
				if(data.respCode=="0"){
					var rsa = new JSEncrypt();
					rsa.setPublicKey($("#key").val());
					that.password = rsa.encrypt(that.password);
					that.$nextTick(function (){
						 that.$refs.loginForm.submit();
					});
				}else{
					$("#error").text(data.respDesc);
				}
			});

			//


		},
		showError:function(info){
			var i = info==undefined?"初始化数据失败":info;
			this.$alert(i, '系统错误', {
		          confirmButtonText: '确定'
		    });			
		}
		
	}
	
});